package com.jn.gsondemo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

public class HotSearchActivity extends AppCompatActivity {
    private ListView hotSearchListView;
    private ArrayList<HashMap<String, String>> hotSearchList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_search);

        hotSearchListView = findViewById(R.id.hot_search_list);
        hotSearchList = new ArrayList<>();

        loadHotSearchData();
        setListViewListener();
    }

    private void loadHotSearchData() {
        try {
            InputStream inputStream = getResources().openRawResource(R.raw.hotsearch);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }

            reader.close();
            String json = stringBuilder.toString();
            JSONArray jsonArray = new JSONArray(json);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                HashMap<String, String> map = new HashMap<>();
                map.put("title", obj.getString("title"));
                map.put("link", obj.getString("link"));
                hotSearchList.add(map);
            }

            SimpleAdapter adapter = new SimpleAdapter(
                    this, hotSearchList,
                    android.R.layout.simple_list_item_1,
                    new String[]{"title"},
                    new int[]{android.R.id.text1});
            hotSearchListView.setAdapter(adapter);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setListViewListener() {
        hotSearchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String, String> map = hotSearchList.get(position);
                String link = map.get("link");
                Intent intent = new Intent(Intent.ACTION_VIEW);
                if (link != null && !link.isEmpty()) {
                    Uri uri = Uri.parse(link.startsWith("http") ? link : "http://" + link);
                    intent.setData(uri);
                    startActivity(intent);
                }
            }
        });
    }
}